from pyzbar.pyzbar import decode
from PIL import Image

def read_qr_from_image(image_path):
    try:
        # 打开图片
        img = Image.open(image_path)
        # 解码
        decoded_objects = decode(img)
        
        if not decoded_objects:
            print(f"在图片 '{image_path}' 中未找到二维码。")
            return

        print(f"在图片 '{image_path}' 中找到 {len(decoded_objects)} 个二维码:")
        for obj in decoded_objects:
            # 二维码数据是字节串，需要解码为字符串
            qr_data = obj.data.decode('utf-8')
            print(f" - 类型: {obj.type}, 数据: '{qr_data}'")
            
    except FileNotFoundError:
        print(f"错误：文件 '{image_path}' 未找到。")
    except Exception as e:
        print(f"发生错误: {e}")

if __name__ == '__main__':
    read_qr_from_image('my_qr.png')